package cn.wokoba.dao.mapper;


import cn.wokoba.dao.pojo.Task;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

//任务表，发送MQ
@Mapper
public interface TaskMapper extends BaseMapper<Task> {

    @Update("""
              update task
                    set state = 'completed', update_time = now()
                    where user_id = #{userId} and message_id = #{messageId}\
            """)
    void updateTaskSendMessageCompleted(Task task);

    @Update("""
             update task
                    set state = 'fail', update_time = now()
                    where user_id = #{userId} and message_id = #{messageId}\
            """)
    void updateTaskSendMessageFail(Task task);

    @Select("""
             select user_id, topic, message_id, message
                    from task
                    where state = 'fail' or (state = 'create' and now() - update_time > 6)
                    limit 10\
            """)
    List<Task> queryNoSendMessageTaskList();
}
